﻿using AuroraPlatform.Chaneels;
using AuroraPlatform.Chaneels.ServiceReference;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Browser;
using System.ServiceModel;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace AuroraPlatform.ViewModel
{
    public class WebServiceProxy
    {
        static WebServiceProxy()
        {
            WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);
            WebRequest.RegisterPrefix("https://", WebRequestCreator.ClientHttp);
        }

        private static Services _webService;

        private static Services WebService
        {
            get
            {
                if(_webService == null)
                {
                    _webService = new Services();

                    var binding = new BasicHttpBinding()
                    {
                        MaxBufferSize = int.MaxValue,
                        MaxReceivedMessageSize = int.MaxValue,
                        SendTimeout = TimeSpan.FromMinutes(10),
                        ReceiveTimeout = TimeSpan.FromMinutes(10),
                    };

                    EndpointAddress point = new EndpointAddress(BusinessContext.Singleton.WebServiceUrl);
                    _webService.AuroraService = new WebServiceSoapClient(binding, point);
                }
                return _webService;
            }
        }

        public async Task<ResArray> GetStaticResource()
        {
            GetStaticResourceRequest request = new GetStaticResourceRequest(new GetStaticResourceRequestBody());
            GetStaticResourceResponse response = await WebService.GetStaticResource(request);
            return ResArray.ParseFrom(response.Body.GetStaticResourceResult);
        }

        public async Task<bool> SignLable(string name)
        {
            SignLableRequest request = new SignLableRequest(new SignLableRequestBody(name));
            SignLableResponse response = await WebService.SignLable(request);
            return response.Body.SignLableResult;
        }

        public async Task<bool> UnSignLable(string name)
        {
            UnSignLableRequest request = new UnSignLableRequest(new UnSignLableRequestBody(name));
            UnSignLableResponse response = await WebService.UnSignLable(request);
            return response.Body.UnSignLableResult;
        }

        public async Task<SignLabelArray> SearchProdName()
        {
            SearchProdNameRequest request = new SearchProdNameRequest(new SearchProdNameRequestBody());
            SearchProdNameResponse response = await WebService.SearchProdName(request);

            byte[] array = response.Body.SearchProdNameResult;
            if (array != null && array.Length > 0)
                return SignLabelArray.ParseFrom(array);

            return null;
        }

        public async Task<User> Login(string name, string pwd)
        {
            LoginRequest request = new LoginRequest(new LoginRequestBody(name, pwd));
            LoginResponse response = await WebService.Login(request);
            byte[] array = response.Body.LoginResult;
            if (array != null && array.Length > 0)
                return User.ParseFrom(array);

            return null;
        }

        public async Task<ProdViewArray> SearchProdList(byte[] conditions)
        {
            SearchProdListRequest request = new SearchProdListRequest(new SearchProdListRequestBody( conditions));
            SearchProdListResponse response = await WebService.SearchProdList(request);
            return ProdViewArray.ParseFrom(response.Body.SearchProdListResult);
        }

        public async Task<string> SearchProdListToExcel(byte[] conditions)
        {
            SearchProdListToExcelRequest request = new SearchProdListToExcelRequest(new SearchProdListToExcelRequestBody(conditions));
            SearchProdListToExcelResponse response = await WebService.SearchProdListToExcel(request);
            return response.Body.SearchProdListToExcelResult;
        }

        public async Task<string> SearchProdListToPDF(byte[] conditions)
        {
            SearchProdListToPDFRequest request = new SearchProdListToPDFRequest(new SearchProdListToPDFRequestBody(conditions));
            SearchProdListToPDFResponse response = await WebService.SearchProdListToPDF(request);
            return response.Body.SearchProdListToPDFResult;
        }

        public async Task<UserArray> GetUsers()
        {
            GetUsersRequest request = new GetUsersRequest(new GetUsersRequestBody());
            GetUsersResponse response = await WebService.GetUsers(request);
            return UserArray.ParseFrom(response.Body.GetUsersResult);
        }

        public async Task<string> AddUser(byte[] bytes)
        {
            AddUserRequest request = new AddUserRequest(new AddUserRequestBody(bytes));
            AddUserResponse response = await WebService.AddUser(request);
            return response.Body.AddUserResult;
        }

        public async Task<bool> UpdateUser(byte[] bytes)
        {
            UpdateUserRequest request = new UpdateUserRequest(new UpdateUserRequestBody(bytes));
            UpdateUserResponse response = await WebService.UpdateUser(request);
            return response.Body.UpdateUserResult;
        }

        public async Task<bool> DeleteUser(string id)
        {
            DeleteUserRequest request = new DeleteUserRequest(new DeleteUserRequestBody(id));
            DeleteUserResponse result = await WebService.DeleteUser(request);
            return result.Body.DeleteUserResult;
        }

        public async Task<string> AddProd(byte[] bytes)
        {
            AddProdRequest request = new AddProdRequest(new AddProdRequestBody(bytes));
            AddProdResponse response = await WebService.AddProd(request);
            return response.Body.AddProdResult;
        }

        public async Task<bool> UpdateProd(byte[] bytes)
        {
            UpdateProdRequest request = new UpdateProdRequest(new UpdateProdRequestBody(bytes));
            UpdateProdResponse response = await WebService.UpdateProd(request);
            return response.Body.UpdateProdResult;
        }

        public async Task<bool> DeleteProd(string id)
        {
            DeleteProdRequest request = new DeleteProdRequest(new DeleteProdRequestBody(id));
            DeleteProdResponse result = await WebService.DeleteProd(request);
            return result.Body.DeleteProdResult;
        }

        public async Task<ProdClassArray> SearchProdClass(byte[] conditions)
        {
            SearchProdClassRequest request = new SearchProdClassRequest(new SearchProdClassRequestBody(conditions));
            SearchProdClassResponse response = await WebService.SearchProdClass(request);
            return ProdClassArray.ParseFrom(response.Body.SearchProdClassResult);
        }

        public async Task<ProdViewArray> SearchProdSeries(byte[] conditions)
        {
            SearchProdSeriesRequest request = new SearchProdSeriesRequest(new SearchProdSeriesRequestBody(conditions));
            SearchProdSeriesResponse response = await WebService.SearchProdSeries(request);
            if(response.Body.SearchProdSeriesResult != null 
                && response.Body.SearchProdSeriesResult.Length > 0)
                return ProdViewArray.ParseFrom(response.Body.SearchProdSeriesResult);

            return null;
        }

        public async Task<string> SearchProdSeriesToExcel(byte[] conditions)
        {
            SearchProdSeriesToExcelRequest request = new SearchProdSeriesToExcelRequest(new SearchProdSeriesToExcelRequestBody(conditions));
            SearchProdSeriesToExcelResponse response = await WebService.SearchProdSeriesToExcel(request);
            return response.Body.SearchProdSeriesToExcelResult;
        }

        public async Task<string> SearchProdSeriesToPDF(byte[] conditions)
        {
            SearchProdSeriesToPDFRequest request = new SearchProdSeriesToPDFRequest(new SearchProdSeriesToPDFRequestBody(conditions));
            SearchProdSeriesToPDFResponse response = await WebService.SearchProdSeriesToPDF(request);
            return response.Body.SearchProdSeriesToPDFResult;
        }


        public async Task<PrimaryProd> SearchPrimaryProdInfo(string id)
        {
            SearchPrimaryProdInfoRequest request = new SearchPrimaryProdInfoRequest(new SearchPrimaryProdInfoRequestBody(id));
            SearchPrimaryProdInfoResponse response = await WebService.SearchPrimaryProdInfo(request);
            byte[] result = response.Body.SearchPrimaryProdInfoResult;
            if(result != null && result.Length > 0)
                return PrimaryProd.ParseFrom(response.Body.SearchPrimaryProdInfoResult);
            return null;
        }

        public async Task<string> SearchPrimaryProdInfoToExcel(string id)
        {
            SearchPrimaryProdInfoToExcelRequest request = new SearchPrimaryProdInfoToExcelRequest(new SearchPrimaryProdInfoToExcelRequestBody(id));
            SearchPrimaryProdInfoToExcelResponse response = await WebService.SearchPrimaryProdInfoToExcel(request);
            return response.Body.SearchPrimaryProdInfoToExcelResult;
        }

        public async Task<string> SearchPrimaryProdInfoToPDF(string id)
        {
            SearchPrimaryProdInfoToPDFRequest request = new SearchPrimaryProdInfoToPDFRequest(new SearchPrimaryProdInfoToPDFRequestBody(id));
            SearchPrimaryProdInfoToPDFResponse response = await WebService.SearchPrimaryProdInfoToPDF(request);
            return response.Body.SearchPrimaryProdInfoToPDFResult;
        }


        public async Task<Prod> SearchProdInfo(string id)
        {
            SearchProdInfoRequest request = new SearchProdInfoRequest(new SearchProdInfoRequestBody(id));
            SearchProdInfoResponse response = await WebService.SearchProdInfo(request);
            byte[] result = response.Body.SearchProdInfoResult;
            if (result != null && result.Length > 0)
                return Prod.ParseFrom(response.Body.SearchProdInfoResult);
            return null;
        }

        public async Task<string> SearchProdInfoToExcel(string id)
        {
            SearchProdInfoToExcelRequest request = new SearchProdInfoToExcelRequest(new SearchProdInfoToExcelRequestBody(id));
            SearchProdInfoToExcelResponse response = await WebService.SearchProdInfoToExcel(request);
            return response.Body.SearchProdInfoToExcelResult;
        }


        public async Task<string> SearchProdInfoToPDF(string id)
        {
            SearchProdInfoToPDFRequest request = new SearchProdInfoToPDFRequest(new SearchProdInfoToPDFRequestBody(id));
            SearchProdInfoToPDFResponse response = await WebService.SearchProdInfoToPDF(request);
            return response.Body.SearchProdInfoToPDFResult;
        }

        public async Task<bool> BatchDeleteProd(byte[] bytes)
        {
            BatchDeleteProdRequest request = new BatchDeleteProdRequest(new BatchDeleteProdRequestBody(bytes));
            BatchDeleteProdResponse response = await WebService.BatchDeleteProd(request);
            return response.Body.BatchDeleteProdResult;
        }

        public async Task<string> CustomExportToExcel(byte[] bytes)
        {
            CustomExportToExcelRequest request = new CustomExportToExcelRequest(new CustomExportToExcelRequestBody(bytes));
            CustomExportToExcelResponse response = await WebService.CustomExportToExcel(request);
            return response.Body.CustomExportToExcelResult;
        }

        public async Task<string> CustomExportToPDF(byte[] bytes)
        {
            CustomExportToPDFRequest request = new CustomExportToPDFRequest(new CustomExportToPDFRequestBody(bytes));
            CustomExportToPDFResponse response = await WebService.CustomExportToPDF(request);
            return response.Body.CustomExportToPDFResult;
        }


        public async Task<PrimaryProdArray> SearchRelations(string id)
        {
            SearchRelationsRequest request = new SearchRelationsRequest(new SearchRelationsRequestBody(id));
            SearchRelationsResponse response = await WebService.SearchRelations(request);
            byte[] result = response.Body.SearchRelationsResult;
            if (result != null && result.Length > 0)
                return PrimaryProdArray.ParseFrom(response.Body.SearchRelationsResult);
            return null;
        }

        public async Task<ProdViewArray> SearchNewProds()
        {
            SearchNewProdsRequest request = new SearchNewProdsRequest(new SearchNewProdsRequestBody());
            SearchNewProdsResponse response = await WebService.SearchNewProds(request);
            byte[] result = response.Body.SearchNewProdsResult;
            if (result != null && result.Length > 0)
                return ProdViewArray.ParseFrom(response.Body.SearchNewProdsResult);
            return null;
        }

        public async Task<string> SearchNewProdsToExcel()
        {
            SearchNewProdsToExcelRequest request = new SearchNewProdsToExcelRequest(new SearchNewProdsToExcelRequestBody());
            SearchNewProdsToExcelResponse response = await WebService.SearchNewProdsToExcel(request);
            return response.Body.SearchNewProdsToExcelResult;
        }

        public async Task<string> SearchNewProdsToPDF()
        {
            SearchNewProdsToPDFRequest request = new SearchNewProdsToPDFRequest(new SearchNewProdsToPDFRequestBody());
            SearchNewProdsToPDFResponse response = await WebService.SearchNewProdsToPDF(request);
            return response.Body.SearchNewProdsToPDFResult;
        }
    }
}
